<template>
  <div class="login-container">
    <div class="login-header">
      <el-icon class="back-btn" :size="24" @click="goBack">
        <ArrowLeft />
      </el-icon>
      <h1 class="login-title">登录</h1>
    </div>
    
    <div class="login-content">
      <div class="login-logo">
        <el-icon :size="80">
          <VideoPlay />
        </el-icon>
        <h2>我的抖音</h2>
        <p>记录美好生活</p>
      </div>
      
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        size="large"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
            clearable
            @keyup.enter="handleLogin"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            :loading="loading"
            @click="handleLogin"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        <span>还没有账号？</span>
        <router-link to="/register" class="register-link">
          立即注册
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'
import { ArrowLeft, VideoPlay } from '@element-plus/icons-vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()

const loginFormRef = ref(null)
const loading = ref(false)

// 登录表单数据
const loginForm = reactive({
  username: '',
  password: ''
})

// 表单验证规则
const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度为3-20位', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度为6-20位', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
  } catch (error) {
    return
  }
  
  loading.value = true
  
  try {
    await userStore.login(loginForm)
    
    ElMessage.success('登录成功')
    
    // 登录成功后跳转
    const redirectPath = route.query.redirect || '/'
    router.push(redirectPath)
    
  } catch (error) {
    console.error('登录失败:', error)
    // 错误信息已在请求拦截器中处理
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  if (window.history.length > 1) {
    router.go(-1)
  } else {
    router.push('/')
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  
  // 背景装饰
  &::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 1px, transparent 1px);
    background-size: 50px 50px;
    animation: float 20s ease-in-out infinite;
  }
}

.login-header {
  position: relative;
  z-index: 10;
  display: flex;
  align-items: center;
  padding: 20px;
  
  .back-btn {
    color: $text-white;
    cursor: pointer;
    transition: $transition-fast;
    
    &:hover {
      transform: translateX(-2px);
    }
  }
  
  .login-title {
    flex: 1;
    text-align: center;
    color: $text-white;
    font-size: 20px;
    font-weight: 600;
    margin: 0;
  }
}

.login-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 0 40px;
  position: relative;
  z-index: 10;
}

.login-logo {
  text-align: center;
  margin-bottom: 40px;
  
  .el-icon {
    color: $text-white;
    margin-bottom: 16px;
  }
  
  h2 {
    color: $text-white;
    font-size: 28px;
    font-weight: 700;
    margin: 0 0 8px 0;
  }
  
  p {
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
    margin: 0;
  }
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  border-radius: $border-radius-large;
  padding: 30px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  
  .el-form-item {
    margin-bottom: 24px;
    
    :deep(.el-input__wrapper) {
      border-radius: $border-radius;
      box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
      transition: $transition-fast;
      
      &:hover {
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
      }
    }
    
    :deep(.el-input.is-focus .el-input__wrapper) {
      box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
    }
  }
  
  .login-btn {
    width: 100%;
    height: 50px;
    font-size: 16px;
    font-weight: 600;
    border-radius: $border-radius;
    background: linear-gradient(135deg, $primary-color 0%, $primary-hover 100%);
    border: none;
    transition: $transition-fast;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 8px 20px rgba(254, 44, 85, 0.3);
    }
    
    &:active {
      transform: translateY(0);
    }
  }
}

.login-footer {
  text-align: center;
  margin-top: 30px;
  color: rgba(255, 255, 255, 0.8);
  
  .register-link {
    color: $text-white;
    font-weight: 600;
    text-decoration: none;
    margin-left: 8px;
    transition: $transition-fast;
    
    &:hover {
      color: rgba(255, 255, 255, 0.8);
      text-decoration: underline;
    }
  }
}

@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(-1%, -1%) rotate(1deg);
  }
  50% {
    transform: translate(1%, 1%) rotate(-1deg);
  }
  75% {
    transform: translate(-1%, 1%) rotate(1deg);
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-content {
    padding: 0 20px;
  }
  
  .login-form {
    padding: 24px;
  }
  
  .login-logo {
    margin-bottom: 30px;
    
    .el-icon {
      font-size: 60px;
    }
    
    h2 {
      font-size: 24px;
    }
    
    p {
      font-size: 14px;
    }
  }
}
</style> 